<?php
/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 2016/10/19
 * Time: 上午11:49
 */

namespace common\repositories\common;


use common\entities\common\MyscrmContractProjectEntity;
use common\repositories\BaseRepository;

class MyscrmContractProjectRepository extends BaseRepository
{
    /**
     * 根据orgcode获取所有信息，过滤掉未授权应用
     * @param array $orgcodes
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getAllByOrgCodes($orgcodes = [])
    {
        $query = MyscrmContractProjectEntity::find();
        $query->select('myscrm_contract_project.*')
            ->leftJoin('myscrm_application', 'myscrm_contract_project.applicationCode=myscrm_application.UniqueName')
            ->where('myscrm_application.IsUnAuth=0');
        if (!empty($orgcodes)) {
            $query->andWhere(["orgcode" => $orgcodes]);
        }
        return $query->asArray()->all();
    }

    public function getAllByOrgCodesAndContractId($contractId, $orgCode)
    {
        return MyscrmContractProjectEntity::find()
            ->where(['orgcode' => $orgCode])
            ->andFilterWhere(['contractId' => $contractId])
            ->asArray()
            ->all();
    }

    /**
     * 根据orgcode获取项目(去重)
     * @param $orgCode
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getProjectByOrgCode($orgCode)
    {
        $query = MyscrmContractProjectEntity::find();
        return $query->select("distinct(ProjId),ProjName")
            ->where(["orgcode" => $orgCode])
            ->asArray()->all();
    }

    /**
     * 获取需要授权的项目列表，已过期，即将过期，未禁用
     * @param $orgCode
     * @param $appCode
     * @param $status
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getNeedAuthorizeProjectList($orgCode, $appCode, $status, $isOverdue)
    {
        $query = MyscrmContractProjectEntity::find()
            ->select(['ProjId', 'ProjName'])
            ->where([
                "orgcode"         => $orgCode,
                "applicationCode" => $appCode, //字符串＆数组
                "isOverdue"       => $isOverdue,
            ])
            ->andFilterWhere([
                'status' => $status,
            ]);

        if ($isOverdue) {
            $query->andWhere([
                '>', 'overdueTime', date('Y-m-d H:i:s', time() - 90 * 86400),
            ]);
        }

        //exit($query->createCommand()->getRawSql());

        return $query->distinct()->asArray()->all();
    }
    public function getAuthorizeProjectList($orgCode, $appCode, $status, $isOverdue, $keyword )
    {
        $query = MyscrmContractProjectEntity::find()
                 ->select(['ProjId', 'ProjName','contractBeginOn','contractEndOn','status'])
                 ->where([
                    "orgcode"         => $orgCode,
                    "applicationCode" => $appCode, //字符串＆数组
                    "isOverdue"       => $isOverdue,
                 ]);
        if(!empty($keyword)) {
            $query = $query->andWhere(['like','ProjName',$keyword]);
        }
        if(!empty($status)) {
            $query = $query->andFilterWhere([
                'status' => $status,
            ]);
        }
        if ($isOverdue) {
            $query = $query->andWhere([
                '>', 'overdueTime', date('Y-m-d H:i:s', time() - 90 * 86400),
            ]);
        }
        return $query->distinct()->asArray()->all();
    }
    public function getPrjectNameListByContractId($contract_id)
    {
        return MyscrmContractProjectEntity::find()
            ->select('ProjName')
            ->where(['contractId' => $contract_id])
            ->distinct()
            ->asArray()
            ->all();
    }
}